//index.js
//获取应用实例
const app = getApp()
const homeData = require('../../antiqueData/antiqueData.js');
var util = require('../../utils/util.js')

Page({
  data: {
    selected: true,
    selected1: false,
    isLoading: true,
    searchPanelShow: false,
    isEmpty: true,
    hasUserInfo: false,
    userInfo: {},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    homeAntique: [],
    totalCount: 1,
    requestUrl: "",

    // name: '',
    // headImg: '',
    cancel: '../../images/index/xx.png',
    classify: [
      {
        text:'云翠珠宝',
        identifier: 1
      },
      {
        text:'紫砂陶器',
        identifier: 1
      },
      {
        text:'书画篆刻',
        identifier: 2
      },
      {
        text:'古币银元',
        identifier: 2
      },
      {
        text:'文玩杂项',
        identifier: 3
      },
      {
        text:'茶酒木艺',
        identifier: 3,
      },
      {
        text:'铜器铁器',
        identifier: 4
      },
      {
        text:'硬币纸品',
        identifier: 4
      },
    ]
  },
  
  //事件处理函数
  selected: function(){
  	this.setData({
  		selected: true,
      selected1: false,
      selected2: false
  	})
  },
  selected1: function(){
  	this.setData({
  		selected: false,
  		selected1: true,
      selected2: false
  	})
  },
  selected2: function(){
    this.setData({
      selected: false,
      selected1: false,
      selected2: true
    })
  },

  onBindFocus: function(){
    this.setData({
      isLoading: true,
      searchPanelShow: true
    })
  },

  // 关键字搜索
  onBindChange: function(e){
    var text = e.detail.value;
    console.log(text);
  },
  onCancelImg: function(){
    this.setData({
      isLoading: false,
      searchPanelShow: false,

    })
  },

  // 详情页面展示
  onAntiqueTap: function(event){
    var antiqueId = event.currentTarget.dataset.antiqueid;
    wx.navigateTo({
      url: "../detail/detail?id=" + antiqueId 
    })
  },

  // 它的发布
  taPublish: function(event){
    var userId = event.currentTarget.dataset.userid;
    wx.navigateTo({
      url: "../thirdPublish/thirdPublish?userid=" + userId 
    })
  },


  //分类
  onMoreTap: function(event){
    var category = event.currentTarget.dataset.category;
    wx.navigateTo({
      url: "../antiqueClassify/antiqueClassify?category=" +  category
    })
  },



  
  onLoad: function (options) {
    
    var dataUrl = app.globalData.antiqueBaseUrl + "topic.html";
    this.data.requestUrl = dataUrl;
    util.http(dataUrl, this.initData); 
    
  },



  initData: function(home) {
    var _this = this;
    if(home.length){
      setTimeout(() => {
        _this.setData({
          isLoading: false
        })
      }, 300)
      var homeAntique = [];
      home.forEach((antique)=>{
        // var avatar1 = app.globalData.antiqueImgUrl + antique.user.headImg;
        var subject = antique.imgs;
        var threeImg = [];
        for (var i in subject){
          var item1 = subject[i];
          var item2 = app.globalData.antiqueImgUrl + item1;
          threeImg.push(item2);
        }
        var temp = {
          antiqueId: antique.id,
          userId: antique.userId,
          avatar: antique.user.headImg,
          typeId: antique.typeId,
          nickName: antique.user.name,
          publishTime: antique.times,
          antiqueName: antique.type.name,
          antiqueDesc: antique.content,
          browserNum: antique.views,
          showImg: threeImg
        }
        homeAntique.push(temp);
      })

      var totalAntiques = {};

      if(!this.data.isEmpty){
        totalAntiques = this.data.homeAntique.concat(homeAntique);
      }else{
        totalAntiques = homeAntique;
        this.data.isEmpty = false;
      }

      this.setData({
        homeAntique: totalAntiques
      });
      this.data.totalCount += 1;
      
    }else{
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }
    
  },

  onScrollLower: function(event){ 
    var nextUrl = this.data.requestUrl + "?pageNo=" + this.data.totalCount + "&pageSize=2";
    util.http(nextUrl, this.initData);
    //在请求的时候，在当前页面显示导航条加载动画，setdata的时候结束导航条加载动画
    wx.showNavigationBarLoading()
  },

  onReady: function(){
    var that = this;
    app.getUserInfo(function (userInfo){
      var name = userInfo.nickName;
      var headImg = userInfo.avatarUrl;

      that.addUser(name,headImg);
    })
  },

  addUser: function(name,headImg){
    console.log(name);
    var addUserUrl = app.globalData.antiqueBaseUrl + "addUser.html";
    var openId = wx.getStorageSync('openid');
    //console.log('openId: '+ openId)
    util.httpAddUser(addUserUrl,openId,name,headImg);
  },

  submit: function(e){
   console.log("formId: " + e.detail.formId);
  }
})
